1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<template>
  <header class="header">
    <h1>todos</h1>
    <input id="toggle-all" class="toggle-all" type="checkbox" v-model="all" />
    <label for="toggle-all"></label>
    <input
      class="new-todo"
      placeholder="输入任务名称-回车确认"
      autofocus
      v-model="task"
      @keyup.enter="enterFn"
      @keyup.esc="clearFn"
    />
  </header>
</template>

<script>
export default {
  name: 'TodoHeader',

  data() {
    return {
      task: ''
    }
  },

  methods: {
    clearFn() {
      this.task = ''
    },

    enterFn() {
      if (this.task.trim() === '') return

      this.$store.commit('main/addList', this.task)

      this.clearFn()
    }
  },

  computed: {
    all: {
      get() {
        return this.$store.getters.rlist[0]
          ? this.$store.getters.rlist.every((obj) => obj.isDone)
          : false
      },
      set(all) {
        this.$store.commit('main/allList', all)
      }
    }
  }
}
</script>